<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>phpsocket.io demo</title>
</head>

<body>

    <input type="button" value="发送hello" id="send_hello">
    <input type="button" value="发送ping" id="send_ping">
    <input type="button" value="访问index/test" id="visit">

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/socket.io/2.3.0/socket.io.dev.js"></script>
    <script>
        // 如果服务端不在本机，请把127.0.0.1改成服务端ip
        var socket = io('http://192.168.56.144:3120');
        // 当连接服务端成功时触发connect默认事件
        socket.on('connect', function () {
            console.log('connect success');
        });
        socket.on('hello', function (data) {
            console.log('hello event', data);
        });
        socket.on('ping2', function (data) {
            console.log('ping event', data);
        });
        socket.on('message', function (data) {
            console.log('message', data);
        });
        socket.on('ttl', function (data) {
            console.log('ttl', data);
        });
    </script>

    <script>
        $(document).ready(function () {
            $('#send_hello').click(function () {
                socket.emit("hello", "hello" + new Date().getTime());
            });
            $('#send_ping').click(function () {
                socket.emit("ping2", "ping");
            });
            $('#visit').click(function () {
                socket.emit("message", JSON.stringify({controller:'index',action:'test',data:'123'}));
                // socket.emit("message", 'aaa');
            });

            //ttl
            setInterval(function(){
                socket.emit("ttl", "ttl" + new Date().getTime());
            },50000);
        });
    </script>
</body>

</html>